//
//  SecondTableviewContentViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SecondTableviewContentViewController.h"
#import "SecondContentModel.h"
#import "SecondCpntentTableviewCell.h"
#import "WebViewViewController.h"
@interface SecondTableviewContentViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSInteger _page;
}
@property (nonatomic, retain)UITableView *tableview;//创建tableview
@property (nonatomic, retain)UIImageView *bgimg;//图片
@property (nonatomic, retain)UILabel *labelcontent;//内容
@property (nonatomic, retain)UILabel *titlelabel;//标题
@property (nonatomic, retain)NSMutableArray *dataArr;//数据数组
@property (nonatomic, retain)UIImageView *headerimg;//头部图片
@property (nonatomic, retain)UILabel *headertitle;//头部图片上的标题
@property (nonatomic, retain)UILabel *headercontent;//头部上的简介
@end

@implementation SecondTableviewContentViewController

-(void)dealloc{
    [_tableview release];
    [_bgimg release];
    [_labelcontent release];
    [_titlelabel release];
    [_dataArr release];
    [_headercontent release];
    [_headerimg release];
    [_headertitle release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //数据
    [self data];
    //创建tableview
    self.tableview = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    //代理
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    //把tableview添加到View上
    [self.view addSubview:self.tableview];
    //释放
    [_tableview release];
    //重用池
    [_tableview registerClass:[SecondCpntentTableviewCell class] forCellReuseIdentifier:@"cell"];
    //头部区域
    [self creat];

}
#pragma mark - tableview的两个必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     SecondContentModel *s = _dataArr[indexPath.row];
     SecondCpntentTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //图片
    [cell.img sd_setImageWithURL:[NSURL URLWithString:s.pic]];
    //标题
    cell.titleLabel.text = s.title;
    //内容
    cell.contentLabel.text = s.descriptioncontent;
    //购买数量
    cell.buy.text = [s.sum stringByAppendingString:@"个人购买"];
    //价格
    cell.price.text = [@"￥" stringByAppendingString:s.yh_price];
    return cell;
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}
#pragma mark - 数据
- (void)data
{

    NSString *str = [[@"http://m.shougongke.com/index.php?c=Shiji&a=topicInfo&topic_id=" stringByAppendingString:self.strid] stringByAppendingString:@"&vid=15"];
    
    [AFNnetWork GETwithURL:str completion:^(id result) {
        #pragma mark - 头部数据
        NSDictionary *dic = result[@"data"];
        [self.headerimg sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"host_pic"]]];//头部图片
        self.headercontent.text = [dic objectForKey:@"topic_des"];//头部内容
        self.headertitle.text = [dic objectForKey:@"topic_name"];//头部标题
        //tableview数据
        NSArray *arr = result[@"data"][@"goods"];
        self.dataArr = [SecondContentModel modelHandlerWithArray:arr];
        NSLog(@"%@",arr);
        [_tableview reloadData];
        
    }];
}
#pragma mark - 刷新


#pragma mark - header
- (void)creat
{
    //头部View
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
    //添加
    [self.tableview addSubview:view];
//    view.backgroundColor = [UIColor redColor];
    //作为头部
    self.tableview.tableHeaderView = view;
    
    //头部图片
    self.headerimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
    [view addSubview:self.headerimg];
    
    //头部标题
    self.headertitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 30)];
    self.headertitle.font = [UIFont systemFontOfSize:17];//字体大小
    self.headertitle.font = [UIFont boldSystemFontOfSize:20];//粗体
    self.headertitle.textAlignment = NSTextAlignmentCenter;//居中
    self.headertitle.textColor = [UIColor whiteColor];//字体颜色
    [self.headerimg addSubview:self.headertitle];
    //头部内容
    self.headercontent = [[UILabel alloc]initWithFrame:CGRectMake(0, self.headerimg.frame.size.height, [UIScreen mainScreen].bounds.size.width , 120)];
    self.headercontent.numberOfLines = 0;//不限制行高
    self.headercontent.font = [UIFont systemFontOfSize:14];//字体
    self.headercontent.textColor = [UIColor grayColor];//字体颜色
    [view addSubview:self.headercontent];
    
    [view release];
    [_headerimg release];
    [_headertitle release];
    [_headercontent release];
    
}
#pragma mark - Tableview点击跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewViewController *web = [[WebViewViewController alloc]init];
    SecondContentModel *s = _dataArr[indexPath.row];
    web.strid = s.num_iid;
    [self.navigationController pushViewController:web animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
